package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.response.Result;
import org.chen.task.ArticleDataSyncTask;
import org.chen.task.ImageCleanupTask;
import org.chen.task.UpdateViewCountSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/schedule")
@Slf4j
public class ImageScheduleController {
    @Autowired
    private ImageCleanupTask imageCleanupTask;

    @Autowired
    private ArticleDataSyncTask articleDataSyncTask;

    @Autowired
    private UpdateViewCountSchedule updateViewCountSchedule;

    @PostMapping("/cleanup")
    public Result<Void> triggerCleanup() {
        try {
            // 异步执行清理任务
            CompletableFuture.runAsync(() -> imageCleanupTask.manualCleanup());
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to trigger cleanup", e);
            return Result.failed("Failed to trigger cleanup");
        }
    }

    @PostMapping("/sync")
    public Result<Void> triggerSync() {
        try {
            // 异步执行清理任务
            CompletableFuture.runAsync(() -> articleDataSyncTask.manualCleanup());
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to trigger sync", e);
            return Result.failed("Failed to trigger sync");
        }
    }

    @PostMapping("/viewCount")
    public Result<Void> triggerViewCount() {
        try {
            // 异步执行清理任务
            CompletableFuture.runAsync(() -> updateViewCountSchedule.manualCleanup());
            return Result.success();
        } catch (Exception e) {
            log.error("Failed to trigger viewCount", e);
            return Result.failed("Failed to trigger viewCount");
        }
    }
}
